package com.un10.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.un10.entity.Administrator;

public class LoginInterceptor implements HandlerInterceptor {
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			 Object o) throws Exception {
			  //��ȡ�����RUi:ȥ��http:localhost:8080�ⲿ��ʣ�µ�
			  String uri = request.getRequestURI();
			  //UTL:����login.jsp�ǿ��Թ������ʵģ�������URL���������ؿ���
			  if (uri.indexOf("/login") >= 0) {
			  return true;
			  }
			  //��ȡsession
			  HttpSession session = request.getSession();
			  Administrator user = (Administrator) session.getAttribute("USER_SESSION");
			  //�ж�session���Ƿ����û����ݣ�����У��򷵻�true����������ִ��
			  if (user != null) {
			  //return true�������
			  return true;
			  }
			  //�����������ĸ�����ʾ��Ϣ����ת������¼ҳ��
			  request.setAttribute("msg", "����û�е�¼�����ȵ�¼��");
			  request.getRequestDispatcher("/login.jsp").forward(request, response);
			
			  //return false��������
			  return false;
			  }

	@Override
	public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			Object o, Exception e) throws Exception {
	}

}
